package com.carkeeper.order.service.impl;

import com.carkeeper.order.dao.AccountsMapper;
import com.carkeeper.order.pojo.Accounts;
import com.carkeeper.order.service.AccountsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Map;

@Service
public class AccountsServiceImpl implements AccountsService {

    @Autowired
    private AccountsMapper accountsMapper;

    /**
     * 分页+条件查询结算表
     * @param searchMap
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<Accounts> accountsPage(Map<String, Object> searchMap,int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        Example example = createExample(searchMap);
        return (Page<Accounts>) accountsMapper.selectByExample(example);
    }

    @Override
    public void addAccounts(Accounts accounts) {
        accountsMapper.insertSelective(accounts);
    }

    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Accounts.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 支付单号
            if (searchMap.get("code") != null && !"".equals(searchMap.get("code"))) {
                criteria.andEqualTo("code", searchMap.get("code"));
            }
            // 收款人
            if (searchMap.get("storeName") != null && !"".equals(searchMap.get("storeName"))) {
                criteria.andEqualTo("storeName", searchMap.get("storeName"));
            }
            // 支付类型
            if (searchMap.get("status") != null && !"".equals(searchMap.get("status"))) {
                criteria.andEqualTo("status", searchMap.get("status"));
            }

            // 支付时间
            if (searchMap.get("startTime") != null && !"".equals(searchMap.get("startTime")) && searchMap.get("endTime") != null && !"".equals(searchMap.get("endTime"))) {
                criteria.andBetween("createTime", searchMap.get("startTime"), searchMap.get("endTime"));
            }
        }
        return example;
    }



}
